/**
 * @description 异步数组包装器，用于app，page和component初始化中得函数序列数组包装
 * @version 1.1.0
 * @author Brave Chan on 2021.3
 */
//============================================================
import be_opt from "../constant/be_opt";

/**
 * @description 声明一个异步执行队列
 * @param { (({ next, error, end})=>any)[] } execList 异步执行
 * @param {(...args) => any} complete
 * @returns {{complete, destroy, execList}}
 */
export default function BEAsyncList(execList, complete) {
  if (!Array.isArray(execList)) {
    if (be_opt.debug) {
      throw new Error(
        `Expect the param "execList" is a array, but type is ${typeof execList}`
      );
    }
    return {};
  }
  let list = execList.slice(0);
  const obj = Object.defineProperties(
    {
      complete: null,
      destroy: null,
    },
    {
      __isAsync: {
        value: true,
        writable: false,
        configurable: false,
        enumerable: false,
      },
      execList: {
        get() {
          return list;
        },
        configurable: false,
        enumerable: false,
      },
    }
  );

  obj.complete = complete;

  obj.destroy = function () {
    list = null;
    execList = null;
    if (typeof this.complete) {
      this.complete = null;
    }
  };

  return obj;
}
